package com.gitee.srurl.system.aspect;

import cn.hutool.core.util.StrUtil;
import com.gitee.srurl.core.constant.RedisConstant;
import com.gitee.srurl.core.util.redis.RedisBloomUtil;
import com.gitee.srurl.core.util.ServletUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author zxw
 * @date 2023/4/29 12:59
 */
@Slf4j
@Aspect
@Component
public class ShortUrlRedirectAspect {

    @Pointcut("execution(* com.gitee.srurl.system.controller.open.ShortUrlRedirectController.redirectRealWebSite(..))")
    public void shortUrlRedirectPointCut() {
    }

    @Around("shortUrlRedirectPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {

        HttpServletRequest request = ServletUtil.getRequest();
        // 查询url是否存在 使用布隆过滤器
        String requestURI = request.getRequestURI();
        if (StrUtil.startWith(requestURI, "/")) {
            requestURI = StrUtil.sub(requestURI, 1, requestURI.length());
        }
        boolean contains = RedisBloomUtil.contains(RedisConstant.Bloom.SHORT_ID_BLOOM_BASE_KEY, requestURI);
        if (!contains) {
            return "地址不存在或已失效";
        }
        return point.proceed();
    }
}
